package com.example.servlet;

import com.example.dao.MemberDao;
import com.example.pojo.Member;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/doMember")
public class MemberServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        String methods = req.getParameter("methods");
        MemberDao dao=new MemberDao();
        //out.println(methods);
        switch (methods)
        {
            case "add":
                Member member=new Member();
                member.setName(req.getParameter("name"));
                member.setPhone(req.getParameter("phone"));
                member.setSex(req.getParameter("sex"));
                member.setPass(req.getParameter("pass"));
                member.setAge(Integer.valueOf(req.getParameter("age")));
                if(dao.addMember(member)) out.print("<h3>添加会员成功</h3>");
                else out.print("<h3>会员添加失败</h3>");
                break;
            case "setpass":
                if(dao.setPass(req.getParameter("phone"),
                        req.getParameter("pass")))
                    out.print("<h3>修改密码成功</h3>");
                else out.print("<h3>修改密码失败</h3>");
                break;
            case "setlevel":
                if(dao.setMemberLevel(req.getParameter("phone"),
                        req.getParameter("level")))
                    out.print("<h3>办理会员卡成功</h3>");
                else out.print("<h3>办理会员卡失败</h3>");
                break;
            case "delete":
                if(dao.deleteMember(req.getParameter("phone")))
                    out.print("<h3>删除会员卡成功</h3>");
                else out.print("<h3>删除会员卡失败</h3>");
                break;
            default:
                break;
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
